package com.czy.owner.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.entity.ReturnOrderModel;
import com.czy.owner.ui.order.ReturnOrderDetail;
import com.czy.owner.utils.glide.GlideUtils;
import com.easyrecycleview.adapter.BaseViewHolder;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;

/**
 * Created by wangyang on 2017/5/18 0018.
 */

public class ReturnOrderAdapter extends RecyclerArrayAdapter<ReturnOrderModel.ListBean> {

    private Context context;

    public ReturnOrderAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new OrderViewHolder(parent);
    }

    class OrderViewHolder extends BaseViewHolder<ReturnOrderModel.ListBean> {
        private TextView tvStoreName;
        private ImageView imgProduct;
        private TextView tvProductDescription;
        private TextView tvProductPrice;
        private TextView tvProductCount;
        private TextView tvRufund;
        private TextView tvBottomOrderRight;
        private TextView tvSpac;

        public OrderViewHolder(ViewGroup parent) {
            super(parent, R.layout.item_return_order);
            tvStoreName = $(R.id.tv_store_name);
            imgProduct = $(R.id.img_product);
            tvProductDescription = $(R.id.tv_product_description);
            tvProductPrice = $(R.id.tv_product_price);
            tvProductCount = $(R.id.tv_product_count);
            tvRufund = $(R.id.tv_rufuned);
            tvBottomOrderRight = $(R.id.tv_bottom_order_right);
            tvSpac = $(R.id.tv_spac);
        }

        @Override
        public void setData(final ReturnOrderModel.ListBean data) {
            super.setData(data);
            if (data.getStoreId() != 0) {
                if (data.getFromGoodsId() != 0) {
                    tvStoreName.setText("平台自营");
                } else {
                    tvStoreName.setText(data.getStoreName());
                }
            } else {
                tvStoreName.setText("平台自营");
            }

            tvProductDescription.setText(data.getGoodsName());
            tvProductPrice.setText("退款金额: ￥" + String.format("%.2f", data.getApplyRefundsAmount()));
            tvProductPrice.setTextColor(context.getResources().getColor(R.color.appThemeYellow));
            tvProductCount.setVisibility(View.GONE);
            if(TextUtils.isEmpty(data.getSpecValue())){
                tvSpac.setVisibility(View.GONE);
            }else {
                if(data.getSpecValue().contains("规格:无规格")){
                    tvSpac.setVisibility(View.GONE);
                }else {
                    tvSpac.setVisibility(View.VISIBLE);
                    tvSpac.setText(data.getSpecValue());
                }

            }

            GlideUtils.loadImage(context, data.getGoodsLogo(), imgProduct, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                @Override
                public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                }

                @Override
                public void onLoadingError(String source, Exception e) {

                }
            }, R.mipmap.default_icon, R.mipmap.default_icon);

//            "refundsType": "退货类型，退货returnGoods，收货退款receiveGoods未收货退款notReceiveGoods"
//            "refundsStatus": "退货状态，已申请applied，已拒绝refused，退款退货中inReturn， 完成complete，已撤销canceled"
            String refundsType = data.getRefundsType();
            String refundsStatus = data.getRefundsStatus();
            if (refundsType.equals("returnGoods")) {
                refundsType = "退货";
            } else if (refundsType.equals("receiveGoods")) {
                refundsType = "收货退货";
            } else if (refundsType.equals("notReceiveGoods")) {
                refundsType = "未收货退款";
            }

            if (refundsStatus.equals("applied")) {
                refundsStatus = "已申请";
            } else if (refundsStatus.equals("refused")) {
                refundsStatus = "已拒绝";
            } else if (refundsStatus.equals("inReturn")) {
                refundsStatus = "退款退货中";
            } else if (refundsStatus.equals("complete")) {
                refundsStatus = "完成";
            } else if (refundsStatus.equals("canceled")) {
                refundsStatus = "已撤销";
            }
            tvRufund.setText(refundsType + "————" + refundsStatus);
            tvBottomOrderRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent ti = new Intent(context,ReturnOrderDetail.class);
                    ti.putExtra("refundsOrderId",data.getRefundsOrderId());
                    context.startActivity(ti);

                }
            });

        }
    }
}